﻿
using GSOrganization.Application;

namespace GSOrganization.Application;

/// <summary>
/// 角色管理
/// </summary>
public class SysRoleService : BaseService, ISysRoleService, ITransient, Furion.DynamicApiController.IDynamicApiController
{

    private readonly IRepository<SysRole> _sysrole;
    private readonly IRepository<SysMenuSysRoleSysOperation> _sysMenuSysRoleSysOperation;

    public SysRoleService(IRepository<SysRole> sysrole, IRepository<SysMenuSysRoleSysOperation> sysMenuSysRoleSysOperation)
    {
        _sysrole = sysrole;
        _sysMenuSysRoleSysOperation = sysMenuSysRoleSysOperation;
    }
    /// <summary>
    /// 角色-授权，保存角色的菜单
    /// </summary>
    /// <returns></returns>
    [Microsoft.AspNetCore.Mvc.HttpPost]
    public ResponseModel SetMenu(SysRoleSyeMenuInput entity)
    {
        //根据 角色id，然后获取菜单
        var sysroleid = entity.SysRoleId;
        var collection = _sysMenuSysRoleSysOperation.Where(w => w.IsDel == false && w.SysRoleId == sysroleid).ToList();
        if (collection != null && collection.Count > 0)
        {
            foreach (var item in collection)
            {
                _sysMenuSysRoleSysOperation.Delete(item);
            }
            _sysMenuSysRoleSysOperation.SaveNow();
        }
        if (!string.IsNullOrWhiteSpace(entity.SysMenuId))
        {
            var sysmenuids = entity.SysMenuId.Split(',');
            foreach (var item in sysmenuids)
            {
                var data = new SysMenuSysRoleSysOperation();

                data.Id = Result.GetNewId();
                data.SysMenuId = item;
                data.IsDel = false;
                data.SysRoleId = sysroleid;
                data.CreatedTime = DateTime.Now;
                data.CreateUserId = UserId;

                _sysMenuSysRoleSysOperation.Insert(data);
            }
            _sysMenuSysRoleSysOperation.SaveNow();

        }

        return ResponseModel.Succeed();
    }
    /// <summary>
    /// 获取单个实体
    /// </summary> 
    /// <param name="id">主键</param>
    /// <returns></returns>    
    public SysRoleOutput GetSysRole(string id)
    {
        if (string.IsNullOrWhiteSpace(id))
        {
            return null;
        }
        var item = _sysrole.FirstOrDefault(w => w.IsDel == false && w.Id == id);
        var entity = item.Adapt<SysRoleOutput>();

        return entity;
    }
    /// <summary>
    /// 创建和修改
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    [Microsoft.AspNetCore.Mvc.HttpPost]
    public ResponseModel CreateSysRole(SysRoleOutput entity)
    {

        if (string.IsNullOrWhiteSpace(entity.Id))
        {
            //创建
            //校验
            if (string.IsNullOrWhiteSpace(entity.Name))
            {
                return ResponseModel.Fail("名称不为空");
            }

            var item = entity.Adapt<SysRole>();
            item.Id = Result.GetNewId();
            item.CreatedTime = DateTime.Now;
            item.CreateUserId = UserId;
            _sysrole.Insert(item);
            var num = _sysrole.SaveNow();

            var log = new SysLogInput() { Result = "成功", MenuId = "角色管理", Message = "新增角色，" + item.Name };
            LogModels.WriteLog(log);

            return ResponseModel.Succeed(num);
        }
        else
        {
            //修改
            //校验
            if (string.IsNullOrWhiteSpace(entity.Name))
            {
                return ResponseModel.Fail("名称不为空");
            }
            var item = _sysrole.FirstOrDefault(f => f.Id == entity.Id);
            if (item == null)
            {
                throw Oops.Oh("数据不存在");

            }
            entity.Adapt(item);
            item.UpdateUserId = UserId;
            item.UpdatedTime = DateTime.Now;

            var num = _sysrole.SaveNow();

            var log = new SysLogInput() { Result = "成功", MenuId = "角色管理", Message = "角色修改，" + item.Name };
            LogModels.WriteLog(log);

            return ResponseModel.Succeed(num);
        }


    }

    /// <summary>
    /// 列表
    /// </summary>
    /// <param name="param">列表的参数对象</param>
    /// <returns>列表返回值</returns>
    public ResponseModel<List<SysRoleIndexOutput>> DataSysRole(SysRoleIndexInput param)
    {

        var data = _sysrole.AsQueryable();
        data = data.Where(w => w.IsDel == false);

        var result = data.OrderByDescending(o => o.CreatedTime)
       .Skip((param.Page - 1) * param.Limit)
       .Take(param.Limit)
       .Select(s => new SysRoleIndexOutput
       {
           Id = s.Id,
           Name = s.Name,
           Description = s.Description,
           CreatedTime = s.CreatedTime.ToString(),

       });


        var total = data.Count();

        return new ResponseModel<List<SysRoleIndexOutput>>
        {
            count = total,
            data = result.ToList()
        };

    }
    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public ResponseModel DeleteRole(string id)
    {
        if (!string.IsNullOrWhiteSpace(id))
        {

            var item = _sysrole.FirstOrDefault(a => a.Id == id);
            if (item != null)
            {
                item.IsDel = true;
                item.UpdateUserId = UserId;
                item.UpdatedTime = DateTime.Now;

                _sysrole.UpdateNow(item);


                var log = new SysLogInput() { Result = "成功", MenuId = "人员管理", Message = "删除" + item.Name + ",id为" + id };
                LogModels.WriteLog(log);

            }
            else
            {
                return ResponseModel.Fail("对不起，删除的数据不存在");
            }

        }
        else
        {
            return ResponseModel.Fail("主键不能为空");
        }

        return ResponseModel.SucceedDelete();
    }


}



